<template>
    <div>
        <el-form ref="form" :model="obj.data" label-width="80px">
            <el-form-item label="标题" prop="title" :rules="[formRulesMixin.notNull]">
                <el-input v-model="obj.data.title" clearable autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="类别" prop="type" :rules="[formRulesMixin.notNull]">
                <el-select v-model="obj.data.type" placeholder="选择分类" class="search_select" clearable>
                    <el-option v-for="(item,index) in obj.type_all" :key="index" :label="item.name" :value="item.id" />
                </el-select>
            </el-form-item>
            <el-form-item label="接收方">
                <!-- <el-checkbox-group v-model="obj.data.target"> -->
                    <el-checkbox v-for="(item,index) in globalData.newsTargetList" :key="index" :model-value="(obj.data.target&parseInt(item.value))>0" @change="itemchange($event, item)">{{item.text}}</el-checkbox>
                <!-- </el-checkbox-group> -->
            </el-form-item>
            <el-form-item label="发布时间" prop="day" :rules="[formRulesMixin.notNull]">
                <DatePicker v-model:value="obj.data.day" />
            </el-form-item>
            <el-form-item label="内容" prop="msg" :rules="[formRulesMixin.notNull]">
                <Editer v-model:value="obj.data.msg" width="100%" height="60vh" :maxheight="100000" />
            </el-form-item>
            <el-form-item class="dialog-btns-item">
                <div class="dialog-btns">
                    <span class="btn clear" @click="$emit('close')">取消</span>
                    <span class="btn sub ml10" @click.prevent="upData">提交</span>
                </div>
            </el-form-item>
        </el-form>
    </div>
</template>

<script setup>
    import { reactive,ref,getCurrentInstance,toRaw,inject } from 'vue'
    import Editer from "@/components/editer.vue"
    import DatePicker from '@/components/date_picker.vue'
    let { proxy } = getCurrentInstance()
    let obj = reactive({
        data:{
            id: inject("id").value || 0,
            title: "",
            msg: "",
            type:"",
            target:0,
            day:proxy.$utils.dateFormat(new Date(), "yyyy-MM-dd"),
        },
        type_all:[]
    })

    let init_befor = async () => {
        obj.type_all = await proxy.$utils.getNewsType(proxy)
    }
    init_befor()
    let init = async () => {
        if(obj.data.id > 0){
            proxy.$ajax({
                url: 'news/get',
                data: obj.data,
                bfLoading: true
            }).then(d => {
                if(d.success){
                    obj.data = {...obj.data, ...d.msg}
                    obj.data.target = parseInt(obj.data.target)
                }else{
                    proxy.elMessageMixin(d.err, 'error')
                }
            }).catch(() => { })
        }
    }
    init()
    let itemchange = (t, item) => {
        if(t){
            obj.data.target += parseInt(item.value)
        }else{
            obj.data.target -= parseInt(item.value)
        }
    }
    let upData = () => {
        proxy.$refs['form'].validate(valid => {
            if (valid) {
                proxy.$ajax({
                    url: 'news/design',
                    data: obj.data,
                    bfLoading: true,
                }).then(d => {
                    if(d.success){
                        proxy.elMessageMixin(`${obj.data.id>0?'修改':"添加"}成功`, 'success')
                        proxy.$emit('init')
                        proxy.$emit('close')
                    }else{
                        proxy.elMessageMixin(d.err, 'error')
                    }
                }).catch(() => { })
            }else{
                return false
            }
        })
    }
</script>

<style scoped>
</style>